package org.whb.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.whb.basic.util.AjaxResult;
import org.whb.basic.util.PageList;
import org.whb.org.domain.Department;
import org.whb.org.query.DepartmentQuery;
import org.whb.org.service.IDepartmentService;

import java.util.List;

/**
 * 部门的接口
 */
@RestController
@RequestMapping("/dept")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DeptController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 通过id查询
     * @param id 查询部门的id
     * @return
     */
    @GetMapping("{id}")
    public Department queryById( @PathVariable Long id){
        return departmentService.queryById(id);
    }

    /**
     * 查询全部
     * @return 返回部门所有信息
     */
    @GetMapping
    public List<Department> queryAll(){
        return departmentService.queryAll();
    }

    /**
     *  分页查询
     * //@RequestBody: 从  请求体  中拿出参数，封装成一个对象
     * @param query 分页查询前端返回的条件
     * @return 前端分页查询需要的数据
     */
    @PostMapping("/list")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }

    /**
     *  删除
     * @param id 根据索引删除id
     * @return
     */
    @DeleteMapping("{id}")
    public AjaxResult removeDept(@PathVariable Long id){
        try {
            departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids 批量删除的id
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            departmentService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("删除失败");
        }
    }


    /**
     *  新增或者修改
     * @param dept 新增或者修改的信息
     * @return
     */
    @PutMapping
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addorDept(@RequestBody Department dept){
        try {
            if(dept.getId()==null){//表示新增
                departmentService.add(dept);
            }else {//表示修改
                departmentService.update(dept);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("操作失败");
        }
    }

    /**
     * 得到部门树
     * @return
     */
    @GetMapping("tree")
    public List<Department> queryDeptTree(){
        return departmentService.queryDeptTree();
    }

}
